Managing memory in a computer system

ABSTRACT

Methods, computer program products, and systems for managing memory in a computer system in which memory locations in use at any given time are represented as a set of memory objects in a first object graph. The first object graph includes a system root object associated by references to each of the memory objects. A method includes creating a second root object for the memory so as to form a second object graph for the memory. The method also includes, in response to the dereferencing of a first object from the first object graph, associating the dereferenced first object with the second object graph so that the second object graph includes at least one dereferenced object.

PRIORITY

This application claims priority to Great Britain Patent Application No.1208434.9, filed May 15, 2012, and all the benefits accruing therefromunder 35 U.S.C. §119, the contents of which in its entirety are hereinincorporated by reference.

BACKGROUND

The present invention relates to a computer system, and moreparticularly, to managing memory in a computer system.

Computer systems commonly use a virtual memory management system.Virtual memory management systems use dynamic memory allocationprocesses and garbage collection processes to respectively allocate andreclaim memory allocations. The garbage collection process is arrangedto identify allocated but unusable memory allocations, clear theassociated memory objects they store and return the identified memoryallocations for reuse by reallocation.

The garbage collection process requires significant processing power andmay delay other processing by the computer. The memory efficiency ofapplication programs can be improved so that fewer discarded memoryobjects are produced for the garbage collection process to clear.However, with multiple system or application programs running on a givencomputer, identifying the source of discarded memory objects isdifficult and time consuming.

SUMMARY

Embodiments include methods, computer program products, and systems formanaging memory in a computer system in which memory locations in use atany given time are represented as a set of memory objects in a firstobject graph. The first object graph includes a system root objectassociated by references to each of the memory objects. A methodincludes creating a second root object for the memory so as to form asecond object graph for the memory. The method also includes, inresponse to the dereferencing of a first object from the first objectgraph, associating the dereferenced first object with the second objectgraph so that the second object graph includes at least one dereferencedobject.

BRIEF DESCRIPTION OF THE SEVERAL VIEWS OF THE DRAWINGS

Embodiments of the present invention will now be described, by way ofexample only, with reference to the following drawings in which:

FIG. 1 is a schematic illustration of a computer system comprising avirtual machine in accordance with an embodiment;

FIGS. 2a and 2b are schematic representations of object graphs in thememory of the virtual machine of FIG. 1 in accordance with anembodiment;

FIG. 3 is a table illustrating data extracted from one of the objectgraphs of FIG. 2b in accordance with an embodiment;

FIG. 4 is a flow chart illustrating processing performed in the computersystem in response to the dereferencing of an object in the object graphof FIG. 2a in accordance with an embodiment; and

FIG. 5 is a flow chart illustrating processing performed in response toa garbage collection process performed on the object graph of FIG. 2bresulting in the data of FIG. 3 in accordance with an embodiment.

DETAILED DESCRIPTION OF THE EMBODIMENTS

Embodiments of the present invention are directed to managing memory ina computer system in which memory locations in use at any given time arerepresented as a set of memory objects in a first object graph. Thefirst object graph includes a system root object associated byreferences to each of the memory objects. Methods include creating asecond root object for the memory so as to form a second object graphfor the memory. In response to the dereferencing of a first object fromthe first object graph, the dereferenced first object is associated withthe second object graph so that the second object graph includes one ormore dereferenced objects.

Methods may also include identifying a second object in the first objectgraph that referenced the first object immediately prior to thedereferencing of the first object and creating a reference from thesecond object graph to the second object so as to associate the firstobject in the second object graph with the second object in the firstobject graph. Methods may also include creating a first metadata objectin the second object graph the first metadata object being arranged toprovide the reference from the second object graph to the second objectin the first object graph and to reference the first object in thesecond object graph. Methods may further include identifyingpredetermined metadata for the first object identifying the location ofthe first object in the first object graph immediately prior to thedereferencing; and storing the metadata in the second object graph inassociation with the first object.

The metadata may be stored in a second metadata object associated withthe first object in the second object graph. The metadata may includedata identifying a field in the second object used to reference thefirst object immediately prior to the dereferencing of the first objectfrom the second object.

Methods may also include, in response to a first stage of garbagecollection in which a first set of objects including all objects presentin the first object graph being identified, identifying a second set ofobjects that include objects present in the second object graph and notpresent in the first object graph. Predetermined data associated witheach of the objects in the second set of objects is saved prior to anyfurther stage of garbage collection in which the second set of objectsis deleted from the memory. The predetermined data may include dataidentifying the first object. The predetermined data may include dataidentifying the second object that referenced the first objectimmediately prior to the dereferencing of the first object. Thepredetermined data may include data identifying the field in the secondobject that referenced the first object in the first object graphimmediately prior to the dereferencing.

Other embodiments provide an apparatus for managing memory in a computersystem in which memory locations in use at any given time arerepresented as a set of memory objects in a first object graph thatincludes a system root object associated by references to each of thememory objects. The apparatus may be operable to create a second rootobject for the memory so as to form a second object graph for thememory, and in response to the dereferencing of a first object from thefirst object graph, associate the dereferenced first object with thesecond object graph so that the second object graph includes one or moredereferenced objects.

Further embodiments provide a computer program product for managingmemory in a computer system in which memory locations in use at anygiven time are represented as a set of memory objects in a first objectgraph that includes a system root object associated by references toeach of the memory objects. The computer program product may include acomputer-readable storage medium having computer-readable program codeembodied therewith, the computer-readable program code configured to:create a second root object for the memory so as to form a second objectgraph for the memory; and in response to the dereferencing of a firstobject from the first object graph, associate the dereferenced firstobject with the second object graph so that the second object graphincludes one or more dereferenced objects.

Embodiments of the invention are arranged to preserve data relating tomemory objects removed from the system object graph for use inidentifying the source of such removed objects.

With reference to FIG. 1, a computer 101 is loaded with an operatingsystem 102, which provides a processing platform for one or moreapplication programs. In the present embodiment, the computer is loadedwith a virtual machine environment application program 103, in the formof a Java runtime environment (JRE) application program, arranged toenable a user to run one or more Java virtual machines (JVMs) 104 on thecomputer 101. Each JVM 104 includes virtual memory 105 and a memorymanager program 106 that includes a garbage collection module 107. EachJVM 104 further includes storage 107 arranged to store one or moreprograms 108 for running on the JVM 104 and to store input or outputprogram data 109.

The memory manager 106 is arranged to manage the use of the memory 105during the processing by the JVM 104 of one of the programs 108 byallocating or de-allocating portions of the memory 105 to the program108 as required. The memory manager 106 periodically performs a garbagecollection process to scan the memory 105 to identify portions of thememory 105 that have been de-allocated by a program 108, clear data fromany such portions of memory and mark the portions of memory as availablefor further allocation by the memory manager 106 to a program 108.

In the memory model for the JVM 104 the memory 105 is initially free andallocated to a memory management data construct call the heap from whichlocations in the memory 105 are allocated on request from a program 108for storage of a memory object in the form of a variable, function ordata structure. With reference to the embodiment shown in FIG. 2a , thememory objects 201 in use at any given time by a program 108 may beassociated by means of references 202 from one object 201 to another soas to form a first object graph 203 in the form of a system objectgraph. The system object graph 203 shown in FIG. 2a includes a rootobject 204 referred to as the system root. In other words, the currentor live data objects for a program 108 are all associated by references202 to the system root 204 either directly or indirectly via one or moreother objects 201. Any given object 201 may be referenced by any numberof other objects 201. In other words, a given object 201 may be linkedor associated with any number of other objects 201 by references fromeach of those other objects 201.

When a reference 202 to a given object 201 is no longer required by aprogram 108, the relevant reference 202 is removed from the object graph203. In other words, the object 201 is de-referenced. However, since anobject 201 may be referenced by any number of other objects 201, theremoval of one reference does not necessarily indicate that the objecthas been discarded. Only once all references 202 to a given object havebeen removed can the object 201 be treated as discarded. Since allreferences 202 to a discarded object 201 will thus have been removed,the discarded object will no longer be linked or associated with thesystem root 204. The garbage collection process of the memory manager106 is arranged to traverse the object graph for the memory 105 andidentify all objects 201 that are linked either directly or indirectlyto the system root 204. Such objects are referred to as live objects,that is, objects that are currently in use by the relevant program 108.All other objects 201 are treated as discarded or dead objects and canthus be removed from memory 105 and their allocated memory returned tothe heap for reuse.

In an embodiment, the memory manager 106 is arranged to collect data forobjects 201 in response to their dereferencing. During the garbagecollection process, the memory manager 106 is further arranged, for anydereferenced object that is dead and to be discarded, to save apredetermined set of data relating to the dead object in the form ofdead object data 110 shown in FIG. 1. In an embodiment, the dead objectdata includes an identification of the dead object, a reference to thelive object from which the dead object was de-referenced, and fieldmetadata identifying the field in the live object that contained theremoved reference to the dead object.

With reference to the embodiment shown in FIG. 2b , in order to collectthe dead object data 110, the memory manager 106 is arranged to maintaina second object graph 205 for the memory 105 which includes thedereferenced objects. In an embodiment, the second object graph 205includes a root object 206 referred to herein as the dead root. Thememory manager 106 is arranged, in response to the dereferencing of anobject from the first object graph 203, that is, the system root objectgraph, to add the dereferenced object to the dereferenced object graph205. In an embodiment, this is implemented by adding a new object to thedereferenced object graph 205 in the form of a dead object informationobject 207. In an embodiment, all new dead object information objectsare referenced directly from the dead root object 206. The dead objectinformation object 207 includes a reference 208 to the newlydereferenced object 209. In an embodiment, the dead information object207 further includes a reference 210 to the live object 201 from whichthe dereferenced object 209 was de-referenced. A field definition object211 is also added to the dereferenced object graph 205 and referencedfrom the dead object information object 207. The field definition object211 includes data identifying the field in the live object 201 thatcontained the removed reference to the newly dereferenced object 209.

The garbage collection process performed by the memory manager 106 isarranged, in a first phase commonly referred to as the marking phase, tosearch the system root object graph 203 and mark all connected objectspresent as “live”. In other words, all objects that are referencedeither directly from the system root 204 or indirectly from the systemroot 204 via one or more other objects 201 in the system root objectgraph 203 are identified as currently in use by a loaded program 108.The dereferenced object graph 205 not used for identifying “live”objects. Nevertheless, some of the objects identified via the systemroot object graph 203 may also be present in the dereferenced objectgraph 205. This occurs where a dereferenced object is referenced by morethan one live object and one or more such references remain when thegarbage collection process is performed.

In a second phase of the garbage collection process, the memory manager106 is arranged to search the dereferenced object graph 205 to identifyany connected object 209 that was not identified as “live” in the firstphase and is thus a candidate for removal or sweeping form the memory105. The garbage collection is arranged to exclude from this search anymetadata objects, that is, in the present embodiment, the deadinformation objects 207 and the field definition objects 211. Thisavoids the unnecessary processing by the garbage collection process ofmetadata objects, which, in the present embodiment, are never referencedby a “live” object. For each such candidate object the associatedobjects in the form of the dead object information object 207 and themetadata object 211 that were created on dereferencing of the object areidentified and the predetermined dead object data 110 extracted andstored as shown in the embodiment shown in FIG. 3. Thus, for eachrelevant object, the dead object data 110 firstly includes an objectidentifier 301 identifying the dead object 209. Secondly the dead objectdata 110 includes an identification 302 of the original parent 201 ofthe dead object 209 that is the object 201 that originally referencedthe dead object 209. The identification 302 is derived from thereference 210 from the dead object information object 207 to theoriginally referencing object 201. Thirdly the dead object data 110includes referencing field data 303 identifying the field in the object201 that was used for originally referencing the dead object 209. Thereferencing field data 303 is extracted from the field definition object211 referenced from the dead object information object 207.

Some objects may be present in the dereferenced object graph 205 thatremain referenced from one or more live objects in the system rootobject graph 203 and thus remain “live”. However, in the first phase ofthe garbage collection process the metadata objects associated with suchdereferenced but still “live” objects will not get marked as “live” asthey are inaccessible from the system root 204 and will thus becandidates for removal. Therefore, in order to preserve the metadataobjects for such dereferenced but still “live” objects, the memorymanager 106 is further arranged to identify any of the dereferencedobjects in the second object graph 205 marked as “live” and to similarlymark their associated metadata objects as “live” to avoid theirsubsequent removal.

Once the dead object data 110 has been extracted from the relevant nodesof the dereferenced object graph 205 a third phase in the garbagecollection process is initiated, commonly referred to as the sweepingphase. In this third phase, all objects in the memory 105 are scannedand all objects not marked as “live” are removed and their memoryallocation returned to the heap for reuse. Where objects are removedfrom memory 105, the dereferenced object graph 205 is repaired to removethe now redundant metadata objects, that is, in an embodiment, therelevant dead information objects 207 and the field definition objects211. Some objects may remain in the dereferenced object graph 205 asthey are still referenced from one or more live objects in the systemroot object graph 203.

An embodiment of the processing performed by the memory manager 106 inresponse to the dereferencing of an object will now be described withreference of the flow chart of FIG. 4. Processing is initiated at block401 in response to the selection of an object for dereferencing andprocessing then moves to block 402. At block 402, the selected object isdereferenced from the system root object graph 203 and processing movesto block 403. At block 403, if no dead root object currently exists inthe memory 105 then processing moves to block 404. At block 404, a deadroot object 206 is created and processing moves to block 405. If, atblock 403, a dead root object 206 is identified in the memory 105 thenprocessing moves straight to block 405. At block 405, a new dead objectinformation object 207 is created and a reference is added from the deadroot object 206 to the new dead object information object 207 andprocessing moves to block 406. At block 406, a reference is added fromthe new dead object information object 207 to the live object 201 thatreferenced the newly dereferenced object 209 immediately prior to itsdereferencing and processing moves to block 407. At block 407, areference is added from the new dead object information object 207 tothe newly dereferenced object 209 and processing moves to block 408. Atblock 408, a new metadata object 211 is created and data identifying thefield in the prior referencing object 201 that provided the reference tothe newly dereferenced object 209 and processing moves to block 409. Atblock 409, a reference to the new metadata object 211 is added to thedead object information object 207. Processing then moves to block 410and ends.

An embodiment of the processing performed by the memory manager 106 inthe garbage collection process will now be described with reference ofthe flow chart of FIG. 5. Processing is initiated at block 501 inaccordance with the garbage collection scheduling of the memory manager106 and processing then moves to block 502. At block 502, the systemroot object graph 203 is traversed and all accessible objects marked aslive and processing moves to block 503. At block 503, the dereferencedobject graph 205 is traversed to identify any dereferenced objectsmarked as “live”, identify their associated metadata objects and markthose objects as “live” and processing moves to block 504. At block 504,the dereferenced object graph 205 is traversed to identify any connectedobject 209 that was not identified as “live” in block 502 and is thus acandidate object for removal from memory 105 and processing moves toblock 505. At block 505, for each identified candidate for removal, thedead object data 110 is extracted from the relevant objects in thedereferenced object graph 205 and stored and processing moves to block506. At block 506, all candidate objects for removal identified in block502 are removed from memory and their memory allocations returned to theheap and processing moves to block 507. At block 507, the dereferencedobject graph 205 is repaired where necessary to take into account theremoved objects. Processing then moves to block 508 and ends.

In another embodiment, the dead object data includes an identificationof closest live object that references a given dead object eitherdirectly or indirectly via one or more other dead objects.

In a further embodiment, the memory manager provides memory allocationsfrom two heaps. The first heap provides storage for all objects and thesecond heap provides storage for the dead root object and the metadataobjects such as the dead information objects and the field definitionobjects. In response to the dereferencing of an object in the firstheap, a corresponding set of metadata objects is created in the secondheap including a cross-heap reference to the dereferenced object in thefirst heap. When the garbage collection process is applied to the firstheap any dereferenced object with no further references from liveobjects will be identified as a candidate for removal. The cross-heapreference from the metadata objects in the second heap will not bevisible and thus not disrupt the identification of the dereferencedobject by the garbage collection process. For each object identified asa candidate for removal, the second heap is scanned to identify andoutput the associated object metadata. The cross-heap reference from themetadata to the associated object in the first heap is then removed. Asecond garbage collection process is then performed on the second heapstarting from the dead root and arranged to identify and remove anymetadata objects not connected to a live object, that is, without across-heap reference to a live object.

As will be understood by those skilled in the art embodiments of theinvention are not limited to Java or the JRE and may be applied to anyvirtual machine system or environment. Suitable virtual machine systemsor environments may be arranged to run directly on a computer system orrun on an operating system. In other words, the virtual machine systemor environment may run natively or be hosted. The virtual machines maybe provided by software emulation or hardware virtualization.

As will be understood by those skilled in the art, embodiments of theinvention may be applied to the physical machine environment, that is,to operating systems running directly on a physical computer andproviding a platform for running one or more application programs.

As will be understood by those skilled in the art any suitable level ofdead object data may be provided for a given application. In someapplications, minimal dead object data may be provided which provides asingle data item for each dead object such as an identifier for the deadobject or an identification of the object that referenced the deadobject immediately prior to its dereferencing or an identification ofclosest live object that references a given dead object either directlyor indirectly via one or more other dead objects.

Embodiments of the invention reference the metadata objects associatedwith dereferenced objects from the dead root and the dereferenced objectitself is referenced from the metadata. This avoids the need foradditional fields in objects for referencing their respective metadata.Such additional fields may need to be hidden in some implementations.

Embodiments of the invention are arranged to preserve data relating tothe memory objects that have been created during the processing ofprograms on a computer that have subsequently been removed from memoryas a result of a garbage collection process or other suitable memorymanagement process. The data may include an identification of theremoved objects or detail of the objects that were associated with theremoved object or referenced the removed object. The data may includeidentification of the fields of referencing objects from which theremoved object was referenced. The dead object data can be used foridentifying the programs, parts of programs or other processes thatcreated the respective objects. Such identification is useful formonitoring, measuring, modifying or improving the memory usage of therelevant program, program part or other process. The data may be usedfor reconstructing the system object graph at selected points in theassociated processing.

It will be understood by those skilled in the art that the apparatusthat embodies a part or all of the present invention may be a generalpurpose device having software arranged to provide a part or all of anembodiment of the invention. The device could be a single device or agroup of devices and the software could be a single program or a set ofprograms. Furthermore, any or all of the software used to implement theinvention can be communicated via any suitable transmission or storagemeans so that the software can be loaded onto one or more devices.

While the present invention has been illustrated by the description ofthe embodiments thereof, and while the embodiments have been describedin considerable detail, it is not the intention of the applicant torestrict or in any way limit the scope of the appended claims to suchdetail. Additional advantages and modifications will readily appear tothose skilled in the art. Therefore, the invention in its broaderaspects is not limited to the specific details of the representativeapparatus and method, and illustrative examples shown and described.Accordingly, departures may be made from such details without departurefrom the scope of applicant's general inventive concept.

What is claimed is:
 1. A method of managing memory in a computer systemin which memory locations in use at any given time are represented as aset of memory objects in a first object graph that includes a systemroot object associated by references to each of said memory objects, themethod comprising: creating a second root object for said memory so asto form a second object graph for said memory; and in response to adereferencing of a first object from said first object graph,associating said dereferenced first object with said second object graphso that said second object graph comprises one or more dereferencedobjects.
 2. A method according to claim 1, further comprising:identifying a second object in said first object graph that referencedsaid first object immediately prior to the dereferencing of said firstobject; and creating a reference from said second object graph to saidsecond object so as to associate said first object in said second objectgraph with said second object in said first object graph.
 3. A methodaccording to claim 2, further comprising: creating a first metadataobject in said second object graph, said first metadata object beingarranged to provide said reference from said second object graph to saidsecond object in said first object graph and to reference said firstobject in said second object graph.
 4. A method according to claim 1,further comprising: identifying predetermined metadata for said firstobject that identifies the location of said first object in said firstobject graph immediately prior to said dereferencing; and storing saidmetadata in said second object graph in association with said firstobject.
 5. A method according to claim 4, wherein said metadata isstored in a second metadata object associated with said first object insaid second object graph.
 6. A method according to claim 4, wherein saidmetadata comprises data identifying a field in said second object usedto reference said first object immediately prior to said dereferencingof said first object from said second object.
 7. A method according toclaim 1, further comprising: in response to a first stage of garbagecollection in which a first set of objects comprising all objectspresent in said first object graph is identified, identifying a secondset of objects comprising objects present in said second object graphand not present in said first object graph; and saving predetermineddata associated with each of said objects in said second set of objectsprior to any further stage of garbage collection in which said secondset of objects is deleted from said memory.
 8. A method according toclaim 7, wherein said predetermined data comprises data identifying saidfirst object.
 9. A method according to claim 7, wherein saidpredetermined data comprises data identifying said second object thatreferenced said first object immediately prior to the dereferencing ofsaid first object.
 10. A method according to claim 7, wherein saidpredetermined data comprises data identifying the field in said secondobject that referenced said first object in said first object graphimmediately prior to said dereferencing.
 11. A system for managingmemory in a computer system, the system comprising: a memory, whereinmemory locations in the memory that are in use at any given time arerepresented as a set of memory objects in a first object graph, thefirst object graph including a system root object associated byreferences to each of the memory objects; and a processor, the systemconfigured to: create a second root object for said memory so as to forma second object graph for said memory; and in response to adereferencing of a first object from said first object graph, associatesaid dereferenced first object with said second object graph so thatsaid second object graph comprises one or more dereferenced objects. 12.The system according to claim 11, wherein the system is furtherconfigured to: identify a second object in said first object graph thatreferenced said first object immediately prior to the dereferencing ofsaid first object; and create a reference from said second object graphto said second object so as to associate said first object in saidsecond object graph with said second object in said first object graph.13. The system according to claim 12, wherein the system is furtherconfigured to: create a first metadata object in said second objectgraph said first metadata object being arranged to provide saidreference from said second object graph to said second object in saidfirst object graph and to reference said first object in said secondobject graph.
 14. The system according to claim 11, wherein the systemis further configured to: identify predetermined metadata for said firstobject identifying the location of said first object in said firstobject graph immediately prior to said dereferencing; and store saidmetadata in said second object graph in association with said firstobject.
 15. The system according to claim 14, wherein said metadata isstored in a second metadata object associated with said first object insaid second object graph.
 16. The system according to claim 14, whereinsaid metadata comprises data identifying a field in said second objectused to reference said first object immediately prior to saiddereferencing of said first object from said second object.
 17. Thesystem according to claim 11, wherein the system is further configuredto: in response to a first stage of garbage collection in which a firstset of objects comprising all objects present in said first object graphis identified, identify a second set of objects comprising objectspresent in said second object graph and not present in said first objectgraph; and save predetermined data associated with each of said objectsin said second set of objects prior to any further stage of garbagecollection in which said second set of objects is deleted from saidmemory.
 18. The system according to claim 17, wherein said predetermineddata comprises data identifying said first object.
 19. The systemaccording to claim 17, wherein said predetermined data comprises dataidentifying said second object that referenced said first objectimmediately prior to the dereferencing of said first object.